/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.google.code.twiddling.core.io.text;

import java.util.ArrayList;

/**
 *  * A Title --> short description of the table. 
 *   attributes: h-align(left, right, center, fill)
 *               decoration(underline)
 * 
 * A Subtitle --> the short description below the title.
 *   attributes: h-align
 *               decoration (underline)
 *
 * @author <a href="mailto:howard.gao@gmail.com">Howard Gao</a> 
 *
 */
public class TitleFormat {
	
	Alignment titleAlign;
	Decoration[] titleDecors;
	
	Alignment subTitleAlign;
	Decoration[] subTitleDecors;
	
	public TitleFormat() {
		titleAlign = Alignment.CENTER;
		titleDecors = new Decoration[] { Decoration.UNDERLINE };
		
		subTitleAlign = Alignment.CENTER;
		subTitleDecors = new Decoration[] { Decoration.NONE };
	}

	public void format(ArrayList<String> allLines, TextTable table) {
		int tabWidth = table.getWidth();
		String title = table.getTitle();
		if (title != null && !title.trim().equals("")) {
			formatTitle(allLines, tabWidth, title);
		}
		String subTitle = table.getSubTitle();
		if (subTitle != null && !subTitle.trim().equals("")) {
			formatTitle(allLines, tabWidth, subTitle);
		}
	}

	private void formatTitle(ArrayList<String> buffer, int tabWidth, String title) {
		if (title.length() > tabWidth) {
			String line = title.substring(0, tabWidth);
			buffer.add(line);
			String remainTitle = title.substring(tabWidth);
			formatTitle(buffer, tabWidth, remainTitle);
		} else {
			
			if (titleAlign == Alignment.LEFT) {
				buffer.add(title);
			} else if (titleAlign == Alignment.RIGHT) {
				StringBuffer sb = new StringBuffer();
				int diff = tabWidth - title.length();
				for (int i = 0; i < diff; ++i) {
					sb.append(" ");
				}
				sb.append(title);
				buffer.add(sb.toString());
			} else if (titleAlign == Alignment.CENTER) {
				StringBuffer sb = new StringBuffer();
				int diff = tabWidth - title.length();
				for (int i = 0; i < diff/2; ++i) {
					sb.append(" ");
				}
				sb.append(title);
				buffer.add(sb.toString());
			}
		}
	}

	public static void main(String[] args) {
		TitleFormat format = new TitleFormat();
		ArrayList<String> allLines = new ArrayList<String>();
		
		format.formatTitle(allLines, 32, "abcdefghijklmnopqr");
	    int sz = allLines.size();
	    for (int i = 0; i < sz; ++i) {
	    	System.err.println(allLines.get(i));
	    }
	}
}
